import scrollOpt from '../base/scrollOptimize'
import { getGachaid } from '../tool/gacha';
import util from '../tool/util';
import { UI_Layer } from '../../config/ui_def'
import { Sdk_Platform } from '../../frame/constants';

cc.Class({
    extends: scrollOpt,

    properties: {
        oppoBottomNode: cc.Node,
        ad_node: cc.Node,
        one_node: cc.Node,
        tip_node: cc.Node,
        close_node: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

    },

    // update (dt) {},

    initPage() {
        if (!he.pool.getPool('bagItem')) {
            he.pool.createPoolSync('bagItem', 5, cc.Prefab, 'uimod/bagItem')
        }
    },

    updatePage(param) {
        this.close_node.active = false
        this.scrollInit()
        this.openListen()
        this.adSwitch(true)
        if (param.itemGrp) {
            this.itemGrp = param.itemGrp
        }
        if (param.get_func) {
            this.get_func = param.get_func
        } else {
            this.get_func = null
        }
        if (param.ad_func) {
            this.ad_func = param.ad_func
        } else {
            this.ad_func = null
        }
        this.btnPosInit()
        if (param.gameOver) {
            this.ad_node.active = false
            this.one_node.active = false
            this.close_node.active = true
        }
        this.creatItem(this.itemGrp)
        gg.eventManager.on('rewardTip', () => {
            this.tipAniServer()
        })
    },

    tipAniServer() {
        this.one_node.active = false
        this.ad_node.active = false
        this.close_node.active = true
        this.tip_node.active = true
        this.tipTween = cc.tween(this.tip_node)
            .repeatForever(
                cc.tween()
                    .to(0.25, { opacity: 255, scale: 2.5 })
                    .to(0.25, { scale: 1.2 })
                    .to(0.25, { opacity: 255, scale: 2.5 })
                    .to(0.25, { scale: 1.2 })
                    .delay(1.5)
            )
            .start()
    },

    btnPosInit() {
        switch (common.sdk_env) {
            case Sdk_Platform.VIVO:
                this.ad_node.active = true
                this.one_node.active = true
                this.ad_node.setPosition(-5, -360)
                break
            case Sdk_Platform.OPPO:
                this.ad_node.active = true
                this.one_node.active = false
                this.ad_node.setPosition(-5, -250)
                break
            default:
                this.ad_node.active = true
                this.one_node.active = true
                this.ad_node.setPosition(-5, -360)
                break
        }
    },

    tipInit() {
        this.tip_node.setScale(1.2)
        this.tip_node.active = false
        this.tip_node.opacity = 0
    },


    adSwitch(status) {
        if (status) {
            switch (common.sdk_env) {
                case Sdk_Platform.OPPO:
                    let guideList = he.local.getJson('guideList', 'user')
                    if (!guideList[6]) {
                        return
                    }
                    gg.uiManager.addPage(UI_Layer.Msg, {
                        name: 'chayeAdFakeOPPO',
                        isShowAd: true,
                        isShowBanner: false,
                        adIndex: 11,
                        position: cc.Vec2(0, -450)
                    })
                    break
                case Sdk_Platform.UC:
                    he.ad.showBanner()
                    break
            }
        } else {
            //  关掉广告
            switch (common.sdk_env) {
                case Sdk_Platform.OPPO:
                    gg.uiManager.closePage(UI_Layer.Msg, 'chayeAdFakeOPPO')
                    break
                case Sdk_Platform.UC:
                    he.ad.closeBanner()
                    break
            }
        }
    },
    closeUI() {
        gg.eventManager.off('rewardTip', this.tipAniServer)
        for (let i = (this.content.children.length); i > 0; i--) {
            he.pool.putElement('bagItem', this.content.children[i - 1])
        }
        this.adSwitch(false)
        this.closeListen()
        if (this.tipTween) {
            this.tipTween.stop()
            this.tipTween = null
        }
    },

    closeServer() {
        this.uiBase.closePage()
    },


    creatItem(grp) {


        let itemGrp = grp
        for (let i = 0; i < itemGrp.length; i++) {
            let itemOne = itemGrp[i].split('_')
            let pre = he.pool.getElement('bagItem')
            pre.parent = this.content
            pre.setPosition(cc.Vec2.ZERO)
            let spt = pre.getComponent('bagItem')
            spt.initInfo(parseInt(itemOne[0]), 1, i)
            spt.setNum(itemOne[1])
        }
    },



    clickGet() {
        if (this.get_func) {
            this.get_func()
        } else {
            gg.uiManager.addTip('明天再来领吧')
        }
        this.uiBase.closePage()
    },
    clickAdGet() {
        if (this.ad_func) {
            util.adFakeTip()
            this.ad_func()

        } else {
            gg.uiManager.addTip('明天再来领吧')
        }
        switch (common.sdk_env) {
            case Sdk_Platform.OPPO:
                this.uiBase.closePage()
                break
        }
    },

});
